package cn.kgc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import cn.kgc.entities.Emp;
import cn.kgc.service.EmpService;

@Controller
public class EmpController {
	@Autowired
	private EmpService empService;
	
	@GetMapping("/emp")
	public String getAllEmp(Model model){
		List<Emp> list = empService.getAllEmp();
		model.addAttribute("empList", list);
		
		return "empList";
	}
	
	@GetMapping("/emp/{id}")
	public String getEmpById(@PathVariable("id") Integer id,Model model){
		Emp emp = empService.getEmpById(id);
		model.addAttribute("emp", emp);
		
		Map<String,Object> sexs = new HashMap<>();
		sexs.put("M", "男");
		sexs.put("F", "女");
		sexs.put("O", "妖");
		model.addAttribute("sexs", sexs);
		
		return "emp";
	}
	
	@PostMapping("/emp")
	public String addEmp(Emp emp){
		empService.addEmp(emp);
		return "redirect:/emp";
	}
	
	@PutMapping("/emp")
	public String updateEmp(Emp emp){
		empService.updateEmp(emp);
		return "redirect:/emp";
	}
	
	@DeleteMapping("/emp/{id}")
	public String deleteEmpById(@PathVariable("id") Integer id){
		empService.deleteEmpById(id);
		return "redirect:/emp";
	}
	
	// 
	/**
	 * 跳转到新增页面
	 * @param  参数描述
	 * @return 返回参数描述
	 */
	@GetMapping("/emp/view")
	public String toAddView(Model model){
		Map<String,Object> sexs = new HashMap<>();
		sexs.put("M", "男");
		sexs.put("F", "女");
		sexs.put("O", "妖");
		model.addAttribute("sexs", sexs);
		model.addAttribute("emp", new Emp());
		return "emp";
	}
}
